const express = require('express')
const router = express.Router()

const tokens = {
  admin: {
    token: 'admin-token'
  },
  editor: {
    token: 'editor-token'
  }
}

const users = {
  'admin-token': {
    roles: ['admin'],
    introduction: 'I am a super administrator',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: '作业看板'
  },
  'editor-token': {
    roles: ['editor'],
    introduction: 'I am an editor',
    avatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
    name: 'Normal Editor'
  }
}

module.exports = [
  // user login
  {
    url: '/vue-admin-template/user/login',
    type: 'post',
    response: config => {
      const {username} = config.body
      const token = tokens[username]

      // mock error
      if (!token) {
        return {
          code: 60204,
          message: 'Account and password are incorrect.'
        }
      }

      return {
        code: 20000,
        data: token
      }
    }
  },

  // get user info
  {
    url: '/vue-admin-template/user/info\.*',
    type: 'get',
    response: config => {
      const {token} = config.query
      const info = users[token]

      // mock error
      if (!info) {
        return {
          code: 50008,
          message: 'Login failed, unable to get user details.'
        }
      }

      return {
        code: 20000,
        data: info
      }
    }
  },

  // user logout
  {
    url: '/vue-admin-template/user/logout',
    type: 'post',
    response: _ => {
      return {
        code: 20000,
        data: 'success'
      }
    }
  }
]
router.get('/info', (req, res, err) => {
  console.log('admin info')
  const { token } = req.query
  const info = users[token]
  if (!info) {
    res.send({
      code: 50008,
      message: 'Login failed, unable to get user details.'
    })
  } else {
    res.send({
      code: 20000,
      data: info
    })
  }
})

/**
 * @api {get} http://127.0.0.1:3658/login 管理员登录
 * @apiDescription 管理后台登录
 * @apiName login
 * @apiGroup admin
 * @apiParam (参数) {String} username 用户名
 * @apiParam (参数) {String} password 密码
 *
 * @apiSuccess {int} code 状态码.
 * @apiSuccess {String} data  token.
 * @apiSuccessExample {json} Success-Response:
 *  {
 *      "code" : 20000,
 *      "data" : #243434hoanga
 *
 *  }
 * @apiVersion 1.0.0
 */
router.post('/login', (req, res, err) => {
  console.log('admin login...')
  let username = req.body.username
  let pwd = req.body.password
  if (username !== 'admin') {
    res.end()
  } else if (pwd !== '123456') {
    res.end()
  } else {
    const token = tokens[username]
    if (!token) {
      res.send({
        code: 60204,
        message: 'Account and password are incorrect.'
      })
    } else {
      res.send({
        code: 20000,
        data: token
      })
    }
    // res.send({ token: create_token(32) })
  }
})

function create_token(leng) {
  leng = leng == undefined ? 32 : leng	// 如果没设置token长度自动为32位
  // 预设随机字符串
  let chars = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz23456789'
  let token = ''
  // 以长度单位进行循环
  for (let i = 0; i < leng; ++i) {
    token += chars.charAt(Math.floor(Math.random() * chars.length))
  }
  return token
}

module.exports = router
